package org.os.framework.context.config;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * 请求上下文
 * 
 * @author
 */
public class ServletContext {

    /**
     * 获取request
     * 
     * @return
     */
    public static HttpServletRequest getHttpServletRequest() {
        try {
            RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
            if (null == attributes) {
                return null;
            }
            return ((ServletRequestAttributes) attributes).getRequest();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取response
     * 
     * @return
     */
    public static HttpServletResponse getHttpServletResponse() {
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        if (null == attributes) {
            return null;
        }
        return ((ServletRequestAttributes) attributes).getResponse();
    }
}
